<template>
  <div>
    <div class="table-container" v-if="!is_h5s">
      <el-table
        v-if="modes == 0"
        :height="tableHeight"
        border
        :data="tableData"
        v-loading="loadingPage"
        style="width: 100%"
        ref="tableref"
        @selection-change="handleSelectionChange"
        row-key="id"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column type="index" width="60" align="center" label="序号">
          <template #default="scope">
            <span v-text="getIndex(scope.$index)"></span>
          </template>
        </el-table-column>
        <!-- 排序修改 开始 -->
        <el-table-column min-width="150" prop="status" label="核校状态">
          <template #default="scope">
            <el-text v-if="scope.row.status == 1" class="mx-1" type="warning"
              >未核校</el-text
            >

            <el-text v-if="scope.row.status == 2" class="mx-1" type="primary"
              >已核校</el-text
            >
            <el-text v-if="scope.row.status == 3" class="mx-1" type="danger"
              >已驳回</el-text
            >
            <el-text v-if="scope.row.status == 4" class="mx-1" type="success"
              >已通过</el-text
            >
            <el-text v-if="scope.row.status == 5" class="mx-1" type="danger"
              >问题校</el-text
            >
          </template>
        </el-table-column>
        <el-table-column
          min-width="100"
          prop="province"
          label="省"
        ></el-table-column>
        <el-table-column
          min-width="100"
          prop="city"
          label="所在城市"
        ></el-table-column>
        <el-table-column
          min-width="150"
          prop="county"
          label="所在区县"
        ></el-table-column>
        <el-table-column
          min-width="240"
          prop="school_name"
          label="学校名称"
        ></el-table-column>
        <el-table-column
          min-width="200"
          prop="school_type"
          label="学校类别"
        ></el-table-column>

        <el-table-column
          min-width="100"
          prop="school_num"
          label="学校人数"
        ></el-table-column>
        <el-table-column
          min-width="100"
          prop="class_num"
          label="班级个数"
        ></el-table-column>
        <el-table-column
          min-width="100"
          prop="teacher_num"
          label="教师人数"
        ></el-table-column>
        <el-table-column
          min-width="200"
          prop="director_name"
          label="学校该项目负责人姓名"
        ></el-table-column>
        <el-table-column
          min-width="200"
          prop="director_phone"
          label="学校该项目负责人电话"
        ></el-table-column>
        <el-table-column
          min-width="150"
          prop="job"
          label="职务"
        ></el-table-column>
        <el-table-column
          min-width="180"
          prop="instrument"
          label="学校是否有直播工具"
        >
          <template #default="scope">
            <el-tag type="primary" v-if="scope.row.instrument == '1'"
              >是</el-tag
            >
            <el-tag type="danger" v-if="scope.row.instrument == '2'">否</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          min-width="200"
          prop="grade"
          label="年级"
        ></el-table-column>
        <el-table-column
          min-width="150"
          prop="class_name"
          label="班级"
        ></el-table-column>
        <el-table-column
          min-width="200"
          prop="course"
          label="课程"
        ></el-table-column>
        <el-table-column
          min-width="100"
          prop="some"
          label="第几节"
        ></el-table-column>
        <el-table-column
          min-width="100"
          prop="week"
          label="周几"
        ></el-table-column>
        <el-table-column
          min-width="100"
          prop="tart_time"
          label="上课时间"
        ></el-table-column>
        <el-table-column
          min-width="100"
          prop="end_time"
          label="下课时间"
        ></el-table-column>

        <el-table-column
          min-width="150"
          prop="num_time"
          label="实际上课时间"
        ></el-table-column>
        <el-table-column
          min-width="150"
          prop="zuser_name"
          label="在地老师名称"
        ></el-table-column>
        <el-table-column
          min-width="150"
          prop="user_phone"
          label="联系电话"
        ></el-table-column>
        <el-table-column min-width="100" prop="class_mode" label="班级规模">
        </el-table-column>
        <el-table-column
          min-width="130"
          prop="admission_time"
          label="入校时间"
        ></el-table-column>
        <el-table-column
          min-width="200"
          prop="zxuser_name"
          label="在线区域负责人名称"
        >
        </el-table-column>
        <el-table-column
          min-width="200"
          prop="user_name"
          label="在地工作人员名称"
        ></el-table-column>

        <el-table-column min-width="100" prop="is_status" label="是否排课">
        </el-table-column>
        <el-table-column min-width="150" prop="images" label="入校照片">
          <template #default="scope">
            <div v-if="scope.row.images">
              <el-image
                v-for="(item, index) in scope.row.images"
                :z-index="99999999"
                :preview-teleported="true"
                :key="index"
                style="width: 30px; height: 30px"
                :src="item"
                :preview-src-list="[item]"
              ></el-image>
            </div>
          </template>
        </el-table-column>
        <el-table-column min-width="150" prop="videos" label="入校视频">
          <template #default="scope">
            <el-button
              v-if="scope.row.video"
              @click="rowUrl(scope.row.video)"
              type="primary"
              icon="Search"
              >查看</el-button
            >
          </template>
        </el-table-column>

        <el-table-column min-width="180" prop="createtime" label="资料下载">
          <template #default="scope">
            <span class="table-operate-btn" @click="loadPZip(scope.row)"
              >图片下载</span
            >
            <span class="table-operate-btn" @click="loadVZip(scope.row)"
              >视频下载</span
            >
          </template>
        </el-table-column>
        <el-table-column
          min-width="180"
          prop="createtime"
          label="创建时间"
        ></el-table-column>
        <!-- 排序修改 结束-->
      </el-table>
      <el-table
        :height="tableHeight"
        v-if="modes == 1"
        border
        :data="tableData"
        v-loading="loadingPage"
        style="width: 100%"
        ref="tableref"
        @selection-change="handleSelectionChange"
        row-key="id"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <!--                <el-table-column type="selection" width="55"></el-table-column>-->
        <!-- <el-table-column type="selection" width="60" align="center"></el-table-column> -->

        <el-table-column type="index" width="60" align="center" label="序号">
          <template #default="scope">
            <span v-text="getIndex(scope.$index)"></span>
          </template>
        </el-table-column>

        <!-- 修改排序 开始 -->
        <el-table-column
          min-width="100"
          prop="province"
          label="省"
        ></el-table-column>

        <el-table-column
          min-width="100"
          prop="municipality"
          label="市"
        ></el-table-column>
        <el-table-column
          min-width="100"
          prop="region"
          label="区"
        ></el-table-column>
        <el-table-column
          min-width="200"
          prop="institution"
          label="所属机构"
        ></el-table-column>
        <el-table-column
          min-width="240"
          prop="school_name"
          label="学校名称"
        ></el-table-column>
        <el-table-column
          min-width="100"
          prop="grade"
          label="年级"
        ></el-table-column>

        <el-table-column
          min-width="100"
          prop="calss"
          label="班级"
        ></el-table-column>
        <el-table-column
          min-width="150"
          prop="subject"
          label="科目"
        ></el-table-column>
        <el-table-column
          min-width="200"
          prop="course"
          label="课程"
        ></el-table-column>
        <el-table-column
          min-width="130"
          prop="teaching_mode"
          label="授课模式"
        ></el-table-column>
        <el-table-column
          min-width="130"
          prop="class_session_name"
          label="课节次名称"
        ></el-table-column>
        <el-table-column
          min-width="150"
          prop="calss_time"
          label="上课日期"
        ></el-table-column>
        <el-table-column
          min-width="100"
          prop="calss_time_slot"
          label="上课时间"
        ></el-table-column>
        <el-table-column
          min-width="100"
          prop="calss_time_last"
          label="下课时间"
        ></el-table-column>
        <el-table-column
          min-width="100"
          prop="volunteer_name"
          label="上课志愿者"
        ></el-table-column>
        <!-- 在此填写志愿者归属小队 开始 -->
        <el-table-column
          min-width="150"
          prop="team"
          label="志愿者归属小队"
        ></el-table-column>
        <!-- 在此填写志愿者归属小队 结束 -->
        <el-table-column
          min-width="100"
          prop="volunteer_stauts"
          label="志愿者考勤"
        >
          <template #default="scope">
            <el-tag type="primary" v-if="scope.row.volunteer_stauts == '1'"
              >正常</el-tag
            >
            <el-tag type="danger" v-else-if="scope.row.volunteer_stauts == '2'"
              >异常</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column
          min-width="100"
          prop="local_teachers"
          label="在地教师"
        ></el-table-column>
        <el-table-column
          min-width="130"
          prop="local_teachers_status"
          label="在地教师考勤"
        >
          <template #default="scope">
            <el-tag type="primary" v-if="scope.row.local_teachers_status == '1'"
              >正常</el-tag
            >
            <el-tag
              type="danger"
              v-else-if="scope.row.local_teachers_status == '2'"
              >异常</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column
          min-width="100"
          prop="class_session_status"
          label="课节状态"
        >
          <template #default="scope">
            <el-tag type="primary" v-if="scope.row.class_session_status == '1'"
              >正常</el-tag
            >
            <el-tag type="danger" v-if="scope.row.class_session_status == '2'"
              >异常</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column
          min-width="100"
          prop="abnormal_cause"
          label="异常原因"
        ></el-table-column>
        <el-table-column min-width="130" prop="link" label="课程会议链接">
          <template #default="scope">
            <a :href="scope.row.link" style="color: blue" target="_blank">
              <el-button type="primary">观看课程</el-button>
            </a>
          </template>
        </el-table-column>
        <el-table-column
          min-width="130"
          prop="code"
          label="课程会议号"
        ></el-table-column>
        <el-table-column
          min-width="200"
          prop="zxguser_name"
          label="督课人员"
        ></el-table-column>
        <!-- 修改排序 结束 -->
        <el-table-column
          min-width="200"
          prop="zdquser_name"
          label="在地区域负责人"
        ></el-table-column>
        <el-table-column
          min-width="200"
          prop="user_name"
          label="在地工作人员"
        ></el-table-column>

        <el-table-column min-width="100" prop="status" label="是否代课">
          <template #default="scope">
            <el-tag type="primary" v-if="scope.row.is_daike == '1'"
              >无代课</el-tag
            >
            <el-tag type="danger" v-if="scope.row.is_daike == '2'">代课</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          min-width="200"
          prop="duser_name"
          label="代课人员"
        ></el-table-column>
        <el-table-column min-width="200" prop="daike_remarks" label="代课原因">
          <template #default="scope">
            <el-tag type="primary" v-if="scope.row.is_daike == '1'"
              >无代课</el-tag
            >
            <span v-else>{{ scope.row.daike_remarks || "无" }}</span>
          </template>
        </el-table-column>
        <!-- is_waichu -->
        <el-table-column min-width="100" prop="is_pause" label="是否暂停">
          <template #default="scope">
            <el-tag type="primary" v-if="scope.row.is_pause == '1'"
              >未暂停</el-tag
            >
            <el-tag type="danger" v-if="scope.row.is_pause == '2'">暂停</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          min-width="160"
          prop="pause_remarks"
          label="暂停原因"
        ></el-table-column>
        <el-table-column min-width="100" prop="is_pause" label="是否外出">
          <template #default="scope">
            <el-tag type="primary" v-if="scope.row.is_waichu == '1'"
              >正常</el-tag
            >
            <el-tag type="danger" v-if="scope.row.is_waichu == '2'"
              >外出</el-tag
            >
          </template>
        </el-table-column>

        <el-table-column min-width="200" prop="daike_remarks" label="首课图片">
          <template #default="scope">
            <img
              v-if="scope.row.frist_image"
              :src="scope.row.frist_image"
              alt=""
              style="width: 30px; height: 30px"
              @click="imageShows(scope.row)"
            />
          </template>
        </el-table-column>

        <el-table-column
          min-width="200"
          prop="remarks"
          label="督课人员备注"
        ></el-table-column>
        <el-table-column
          min-width="200"
          prop="zremarks"
          label="在地人员备注"
        ></el-table-column>
      </el-table>
      <el-dialog v-model="videoDialogVisible" width="80%" top="5vh">
        <el-carousel height="900px">
          <el-carousel-item
            v-for="(item, index) in currentVideoUrl"
            :key="index"
          >
            <video
              :src="item"
              controls
              style="width: 100%"
              autoplay="autoplay"
            ></video>
          </el-carousel-item>
        </el-carousel>
      </el-dialog>
    </div>
    <div v-else>
      <div v-if="modes == 0">
        <div v-if="tableData.length">
          <shoolcol
            v-for="(item, index) in tableData"
            :key="index"
            :dataobjs="item"
          ></shoolcol>
        </div>
        <div v-else>
          <el-empty description="暂无数据" />
        </div>
      </div>
      <div v-else>
        <div v-if="tableData.length">
          <courseCol
            v-for="(item, index) in tableData"
            :key="index"
            :dataobjs="item"
          ></courseCol>
        </div>
        <div v-else>
          <el-empty description="暂无数据" />
        </div>
      </div>
    </div>
  </div>
</template>
  
<script>
import axios from "axios";
import shoolcol from "@/components/shoolcol.vue";
import courseCol from "@/components/courseCol.vue";

export default {
  components: {
    shoolcol,
    courseCol,
  },
  props: {
    is_h5s: {
      typeof: Boolean,
      default: false,
    },
    // 0 班级 1督课
    modes: {
      typeof: String | Number,
      default: 0,
    },
    tableData: {
      typeof: Array,
      default: () => [],
    },
    pageparm: {
      typeof: Object,
      default: () => {},
    },
  },
  data() {
    return {
      currentVideoUrl: [], // 当前视频链接
      videoDialogVisible: false,
      loadingPage: false,
      // 选项框选中数组
      ids: [],
      tableHeight: "500",
      // 选项框非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
    };
  },
  methods: {
    loadVZip(e) {
      if (!e.video || e.video.length == 0) {
        return this.$message.error("入校视频暂未上传！！！");
      }

      axios({
        url: this.$config.API_URL + "school/videozip/", // 后端提供的 ZIP 文件下载 URL
        method: "POST",
        responseType: "blob", // 重要，这会返回一个 Blob 对象
        data: {
          id: e.id,
        },
      })
        .then((response) => {
          const url = URL.createObjectURL(new Blob([response.data]));
          const link = document.createElement("a");
          link.href = url;
          link.download = `${e.school_name}${e.grade}${e.class_name}-视频.Zip`; // 设置下载文件名
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
          URL.revokeObjectURL(url); // 释放URL对象
        })
        .catch((e) => {
          console.error("下载失败:", e);
        });
    },

    loadPZip(e) {
      if (!e.images || e.images.length == 0) {
        return this.$message.error("入校照片暂未上传");
      }
      axios({
        url: this.$config.API_URL + "school/imageszip/", // 后端提供的 ZIP 文件下载 URL
        method: "POST",
        responseType: "blob", // 重要，这会返回一个 Blob 对象
        data: {
          id: e.id,
        },
      })
        .then((response) => {
          const url = URL.createObjectURL(new Blob([response.data]));
          const link = document.createElement("a");
          link.href = url;
          link.download = `${e.school_name}${e.grade}${e.class_name}-图.Zip`; // 设置下载文件名
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
          URL.revokeObjectURL(url); // 释放URL对象
        })
        .catch((e) => {
          console.error("下载失败:", e);
        });
    },
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
      console.log(this.ids, "选中项");
    },
    // 表格序列号
    getIndex($index) {
      // (当前页 - 1) * 当前显示数据条数 + 当前行数据的索引 + 1
      return (this.pageparm.page - 1) * this.pageparm.per_page + $index + 1;
    },
    rowUrl(url) {
      this.currentVideoUrl = url;
      this.videoDialogVisible = true; // 显示对话框
    },
  },
};
</script>
<style >
.table-container {
  width: 100%;
  min-width: 500px;
  max-height: 1200px; /* 可选：设置最大高度 */
}

.el-table {
  width: 100%;
  min-width: 100%;
}
</style>